﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;

namespace Jy123.Web.Letter
{
    public partial class Out : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID;//暂时
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);
            string strType = Request.QueryString["Type"];
            string strLetterID = Request.QueryString["LetterID"];
            if (strType == "del" && strLetterID != null)
            {
                DelLetter(strLetterID);
            }
            StringBuilder sbHtml = new StringBuilder();
            sbHtml = CommonHtml(sbHtml, "user_letter_main");//公共部分的html
            sbHtml.Replace("$LetterList$", LetterList(userID));
            sbHtml.Replace("$LetterCount$", LetterCountList(userID));//站内信统计
            sbHtml.Replace("$InorOut$", "Out.aspx");
            sbHtml.Replace("$ClassIn$",string.Empty )
            .Replace("$ClassOut$", "class=\"sel\"")
            .Replace("$ClassNotice$", string.Empty)
            .Replace("$ClassAdd$", string.Empty);
            Response.Write(sbHtml.ToString());
        }
        /// <summary>
        /// 标记或直接删除记录
        /// </summary>
        /// <param name="strLetterID">站内信ID</param>
        private void DelLetter(string strLetterID)
        {
            Jy123.Model.User_Letter mLetter = new Jy123.Model.User_Letter();
            Jy123.BLL.User_Letter bllLetter = new Jy123.BLL.User_Letter();
            mLetter = bllLetter.GetModel(Convert.ToInt32(strLetterID));
            if (mLetter.IsDel == 0)
            {
                mLetter.IsDel = 1;
                bllLetter.Update(mLetter);
            }
            else
            {
                bllLetter.Delete(Convert.ToInt32(strLetterID));
            }
        }
        /// 公共部分的html
        /// </summary>
        /// <param name="sbHtml">最终写出页面的大StringBuilder</param>
        /// <param name="cacheHtml">缓存名称</param>
        /// <returns>StringBuilder</returns>
        private StringBuilder CommonHtml(StringBuilder sbHtml, string cacheHtml)
        {
            sbHtml = new StringBuilder(Cache[cacheHtml].ToString());
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧直接添加好友
            return sbHtml;
        }

        /// <summary>
        /// 站内信列表
        /// </summary>
        /// <param name="userID">当前用户ID</param>
        /// <returns>字符串</returns>
        private string LetterList(int userID)
        {
            int currentpage = 1;//当前页
            int pagesize = 4;//页记录大小

            //把被查询页赋值给当前页
            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                currentpage = Convert.ToInt32(Request.QueryString["page"]);
            }
            List<int?> countLetter, countComment;
            StringBuilder sbHtml = new StringBuilder();
            string strHtml = Cache["user_letterlist"].ToString();
            DataTable dtList = new Jy123.BLL.User.Letter().LetterList(currentpage, pagesize, "FUserSender='" + userID + "'and (IsDel=0 or IsDel=2)", "PostTime DESC", out countLetter).Tables[0];
            for (int i = 0; i < dtList.Rows.Count; i++)
            {
                string str = strHtml;
                str = str.Replace("$LetterID$", dtList.Rows[i]["ID"].ToString());
                str = str.Replace("$UserID$", dtList.Rows[i]["FUserReceiver"].ToString());
                str = str.Replace("$UserName$", dtList.Rows[i]["ReceiverName"].ToString());
                str = str.Replace("$Portrait$", dtList.Rows[i]["ReceiverPortrait"].ToString());
                str = str.Replace("$LetterTitle$", dtList.Rows[i]["Title"].ToString());
                str = str.Replace("$LetterContent$", Jy123.BLL.User.UserSql.BlogContent(dtList.Rows[i]["LetterContent"].ToString()));
                str = str.Replace("$PostTime$", dtList.Rows[i]["PostTime"].ToString());
                str = str.Replace("$LetterCommentList$", LetterCommentList(Convert.ToInt32(dtList.Rows[i]["ID"]),userID, out countComment));
                str = str.Replace("$CommentCount$", countComment[0].ToString());
                string isRead = dtList.Rows[i]["IsRead"].ToString();
                if (isRead == "1")//如果是已经阅读过的
                { str = str.Replace("<b class=\"red\">[未阅读]</b>", "<B class=\"blue\">[已阅]</B>"); }
                sbHtml.Append(str);
            }
            string pageHtml = @"
                        <ul>
                            <li class="">
                                <span>
                                       {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b>
                            </li>
                        </ul>";
            string strHref = "Out.aspx?page=$num$";
            if (countLetter[1] > 1)
            {
                MoonPage mp = new MoonPage(false, true);
                mp.Count = Convert.ToInt32(countLetter[1]);
                sbHtml.AppendFormat(pageHtml, mp.GetStrPage(currentpage, "no", "sel", strHref), 1 + (currentpage - 1) * pagesize, ((currentpage * pagesize) > countLetter[0]) ? countLetter[0] : currentpage * pagesize, countLetter[0]);
            }            
            return sbHtml.ToString();
        }
        /// <summary>
        /// 站内信评论列表
        /// </summary>
        /// <param name="letterID">站内信ID</param>
        /// <returns>字符串</returns>
        private string LetterCommentList(int letterID,int userID,out List<int?> count)
        {
            StringBuilder sbHtml = new StringBuilder();
            string strHtml = Cache["user_lettercommentlistli"].ToString();
            DataTable dtList = new Jy123.BLL.User.Letter().LetterCommentList(1, int.MaxValue, "FLetter='" + letterID + "'", "ID ASC", out count).Tables[0];
            string strDel = "<img src=\"/images/del_blog.gif\" alt=\"删除\" onclick=\"javascript:myAjax('/Letter/CommentDel.ashx','CommentID='+$CommentID$);\" />";
            for (int i = 0; i < dtList.Rows.Count; i++)
            {
                string str = strHtml;
                if (dtList.Rows[i]["FUserCommenter"].ToString().Equals(userID.ToString()))
                {
                    str = str.Replace("$imgdel$", strDel);
                }
                else
                {
                    str = str.Replace("$imgdel$", string.Empty);
                }
                str = str.Replace("$CommentID$", dtList.Rows[i]["ID"].ToString());
                str = str.Replace("$UserID$", dtList.Rows[i]["FUserCommenter"].ToString());
                str = str.Replace("$UserName$", dtList.Rows[i]["UserName"].ToString());
                str = str.Replace("$LetterContent$", Jy123.BLL.User.UserSql.BlogContent(dtList.Rows[i]["CommentContent"].ToString()));
                str = str.Replace("$PostTime$", dtList.Rows[i]["PostTime"].ToString());
                sbHtml.Append(str);
            }
            return sbHtml.ToString();
        }
        /// <summary>
        /// 站内信统计
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>字符串</returns>
        private string LetterCountList(int userID)
        {
            string strHtml = Cache["user_letter_letterCount"].ToString();
            strHtml = strHtml.Replace("$LtCount$", new Jy123.BLL.User.Letter().LetterCount(userID).ToString());
            strHtml = strHtml.Replace("$NoReadCount$", new Jy123.BLL.User.Letter().LetterCountNoRead(userID).ToString());
            strHtml = strHtml.Replace("$ReadedCount$", new Jy123.BLL.User.Letter().LetterCountReaded(userID).ToString());
            return strHtml;
        }
    }
}
